import React,{useEffect,useState,useRef} from 'react'

function useDao(count=10) {
    let [num,setNum]=useState(count)
    let timer=useRef(null)
    let [flag,setFlag]=useState(false)
    const fn=()=>{
        setFlag(true)
        timer.current=setInterval(()=>{
            setNum((c)=>c-1)
        },1000)
    }

    useEffect(()=>{
        if(num<=0){
            clearInterval(timer.current)
            setNum(count)
            setFlag(false)
        }
    },[fn,num])
  return {fn,num,flag}
   
}

export default useDao
